# 音频转文字服务数据模型

from typing import Dict, Any, Optional, List
from pydantic import BaseModel, Field

class AudioMetadata(BaseModel):
    """
    音频元数据模型
    """
    language: str = Field(..., description="识别使用的语言代码")
    duration: float = Field(..., description="音频时长(秒)")
    format: str = Field(..., description="原始音频格式")
    filename: str = Field(..., description="原始文件名")
    model: str = Field(..., description="使用的Whisper模型")
    device: str = Field(..., description="处理设备(CPU/CUDA/MPS)")
    processing_time: float = Field(..., description="处理耗时(秒)")
    file_size: int = Field(..., description="文件大小(字节)")
    sample_rate: Optional[int] = Field(None, description="采样率")
    channels: Optional[int] = Field(None, description="声道数")
    bit_depth: Optional[int] = Field(None, description="位深度")

class ConfidenceAnalysis(BaseModel):
    """
    置信度分析模型
    """
    avg_confidence: float = Field(..., description="平均置信度")
    min_confidence: float = Field(..., description="最低置信度")
    max_confidence: float = Field(..., description="最高置信度")
    confidence_variance: float = Field(..., description="置信度方差")
    low_confidence_segments: int = Field(..., description="低置信度片段数量")
    total_segments: int = Field(..., description="总片段数")
    quality_score: str = Field(..., description="质量评分(high/medium/low)")

class LanguageDetection(BaseModel):
    """
    语言检测信息模型
    """
    detected_language: str = Field(..., description="检测到的语言")
    requested_language: Optional[str] = Field(None, description="请求的语言")
    whisper_language: Optional[str] = Field(None, description="Whisper使用的语言代码")
    auto_detected: bool = Field(..., description="是否为自动检测")

class AudioSegment(BaseModel):
    """
    音频片段模型
    """
    id: int = Field(..., description="片段ID")
    start: float = Field(..., description="开始时间(秒)")
    end: float = Field(..., description="结束时间(秒)")
    text: str = Field(..., description="片段文本")
    confidence: float = Field(..., description="片段置信度")
    avg_logprob: Optional[float] = Field(None, description="平均对数概率")
    no_speech_prob: Optional[float] = Field(None, description="无语音概率")

class WhisperModelInfo(BaseModel):
    """
    Whisper模型信息模型
    """
    name: str = Field(..., description="模型名称")
    size: str = Field(..., description="模型大小")
    vram: str = Field(..., description="显存需求")
    speed: str = Field(..., description="处理速度")
    description: str = Field(..., description="模型描述")

class AudioToTextResponse(BaseModel):
    """
    音频转文字响应模型
    """
    text: str = Field(..., description="识别的文本内容")
    confidence: float = Field(..., description="识别置信度(0-1)")
    metadata: AudioMetadata = Field(..., description="音频元数据")
    segments: Optional[List[AudioSegment]] = Field(None, description="音频片段详情")
    confidence_analysis: Optional[ConfidenceAnalysis] = Field(None, description="置信度分析")
    language_detection: Optional[LanguageDetection] = Field(None, description="语言检测信息")
    
    model_config = {
        "json_schema_extra": {
            "example": {
                "text": "这是一段示例的转换文本内容。",
                "confidence": 0.95,
                "metadata": {
                    "language": "zh-CN",
                    "duration": 5.32,
                    "format": "mp3",
                    "filename": "sample.mp3",
                    "model": "base",
                    "device": "cuda",
                    "processing_time": 2.1,
                    "file_size": 1024000
                }
            }
        }
    }

class BatchProcessResult(BaseModel):
    """
    批处理结果模型
    """
    file_index: int = Field(..., description="文件索引")
    filename: str = Field(..., description="文件名")
    text: str = Field(..., description="识别文本")
    confidence: float = Field(..., description="置信度")
    metadata: Dict[str, Any] = Field(..., description="元数据")
    segments: Optional[List[AudioSegment]] = Field(None, description="音频片段")
    error: Optional[str] = Field(None, description="错误信息")

class BatchProcessResponse(BaseModel):
    """
    批处理响应模型
    """
    total_files: int = Field(..., description="总文件数")
    successful: int = Field(..., description="成功处理数")
    failed: int = Field(..., description="失败处理数")
    results: List[BatchProcessResult] = Field(..., description="处理结果列表")

class ModelSwitchResponse(BaseModel):
    """
    模型切换响应模型
    """
    success: bool = Field(..., description="是否成功")
    message: str = Field(..., description="响应消息")
    current_model: str = Field(..., description="当前模型")
    model_info: WhisperModelInfo = Field(..., description="模型信息")

class SystemStatus(BaseModel):
    """
    系统状态模型
    """
    status: str = Field(..., description="系统状态")
    device_info: Dict[str, Any] = Field(..., description="设备信息")
    system_info: Dict[str, Any] = Field(..., description="系统信息")
    config_info: Dict[str, Any] = Field(..., description="配置信息")
    current_model: str = Field(..., description="当前模型")
    error: Optional[str] = Field(None, description="错误信息")

class ErrorResponse(BaseModel):
    """
    错误响应模型
    """
    detail: str = Field(..., description="错误详情")
    
    model_config = {
        "json_schema_extra": {
            "example": {
                "detail": "请求处理过程中发生错误"
            }
        }
    }